﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDTB.Common;
using LDTB.Common.Entities;

namespace LDTB.UI
{
    public partial class Ward : UIBase
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                
                FillHuyen();
            }
        }

        private void FillHuyen()
        {
            var wards = from w in Data.VC_Huyens
                        orderby w.Name descending
                        select w;
            this.rpt.DataSource = wards;
            rpt.DataBind();

        }

       

        protected void DeleteWard(object sender,EventArgs e) { 
        
            int id = int.Parse((sender as LinkButton).CommandArgument);

            var pls = from p in Data.VC_Peoples
                      join x in Data.VC_Xas
                      on p.XaID equals x.ID
                      where x.HuyenID == id
                      select p;

            Data.VC_Peoples.DeleteAllOnSubmit(pls);

            var vills = from v in Data.VC_Xas
                        where v.HuyenID == id
                        select v;

            Data.VC_Xas.DeleteAllOnSubmit(vills);

            var ward = (from w in Data.VC_Huyens
                       where w.ID == id
                       select w).FirstOrDefault();

            Data.VC_Huyens.DeleteOnSubmit(ward);
            Data.SubmitChanges();

            FillHuyen();
        }
    }
}